﻿using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// PlainTextEditor.xaml 的交互逻辑
    /// </summary>
    public partial class PlainTextEditor : MetroWindow
    {
        /// <summary>
        /// [构造方法]创建一个纯文本编辑器。没有什么语法高亮之类的特殊功能。
        /// </summary>
        /// <param name="filePath">要编辑的文件的完整路径。</param>
        public PlainTextEditor(string filePath)
        {
            InitializeComponent();

            this.Title = Globals.AppName;
            this.filePath = filePath;
        }

        private string filePath;
        /// <summary>
        /// 要编辑的文件的完整路径。
        /// </summary>
        public string FilePath
        {
            get { return filePath; }
        }

        /// <summary>
        /// 放弃编辑，关闭窗口。
        /// </summary>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
            return;
        }

        /// <summary>
        /// 保存文件，关闭窗口。
        /// </summary>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                File.WriteAllText(this.filePath, tbx.Text, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                LMessageBox.Show(ex.Message);
            }

            this.DialogResult = true;
            this.Close();
            return;
        }
    }
}
